package com.ssm.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.ssm.util.object.ObjectConstant;

/**
 * 获取reqmapping.xml的线程
 * @author mps
 *
 */
public class TokenThread implements Runnable {

	private static Logger log = LoggerFactory.getLogger(TokenThread.class);
	
	public static List<Map<String,Object>> reqmapping = null;

	public void run() {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = factory.newDocumentBuilder();
			String filePath = TokenThread.class.getClassLoader().getResource("/mapping/reqpath/reqmapping.xml").getPath();
			Document doc = db.parse(new File(filePath));
			Element controller = doc.getDocumentElement();
			NodeList controllerNodes = controller.getElementsByTagName("action");
			reqmapping = new ArrayList<Map<String,Object>>();
			
			for (int i = 0; i < controllerNodes.getLength(); i++) {
				List<Map<String,Object>> controllerBeans = new ArrayList<Map<String,Object>>();
				Map<String,Object> controllerBean = new HashMap<String, Object>();
				Element action = (Element)controllerNodes.item(i);
				NodeList actionNodes = action.getElementsByTagName("property");
				controllerBean.put("id", ObjectConstant.PROJECT_NAME_Z + action.getAttribute("id"));
				controllerBean.put("path", ObjectConstant.PROJECT_NAME + action.getAttribute("path"));
				
				for(int j = 0;j < actionNodes.getLength(); j++){
					Map<String,Object> actionBean = new HashMap<String, Object>();
					Element property = (Element)actionNodes.item(j);
					actionBean.put("id", property.getAttribute("id"));
					actionBean.put("name", property.getAttribute("name"));
					actionBean.put("ref", property.getAttribute("ref"));
					controllerBeans.add(actionBean);
				}
				
				controllerBean.put("property", controllerBeans);
				reqmapping.add(controllerBean);
			}
			log.info("请求路径文件读取完毕");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
